package com.cac.demo.util;


import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cac.demo.dao.materialReceive.PackageUnpackDao;
import com.cac.demo.dao.outBill.WmsInvOutBillHDao;
import com.cac.demo.dao.pointPackageSort.PointPackageDao;
import com.cac.demo.newCode.cemop.dto.CemopCancelSupportingLedgerDTO;
import com.cac.demo.newCode.cemop.dto.CemopSupportingLedgerDTO;
import com.cac.demo.newCode.cemop.service.CemopService;
import com.cac.demo.newCode.sorting.entity.MsAoMatchBill;
import com.cac.demo.newCode.sorting.entity.WmsInvOutBillH;
import com.cac.demo.newCode.sorting.entity.WmsInvOutBillL;
import com.cac.demo.service.CallInterface.CallInterfaceService;
import com.cac.demo.service.materialReceive.MaterialReceiptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Component
@Slf4j
public class PackageUnpackServiceUtil {

    @Autowired
    private WmsInvOutBillHDao wmsInvOutBillHDao;
    @Autowired
    private MaterialReceiptService materialReceiptService;
    @Autowired
    private CallInterfaceService callInterfaceService;
    @Autowired
    private PackageUnpackDao packageUnpackDao;
    @Autowired
    private PointPackageDao pointPackageDao;
    @Resource
    CemopService cemopService;

    /***
     * 取消占用以及重新占用
     * */
    @Async
    public void unpackAllot(HashMap map, String userCode, String IP) {
        //根据台账id查询占用信息
        List<HashMap> params = packageUnpackDao.queryUnpackCancelAllot(map);
        for(HashMap param : params){
            HashMap cancel = new HashMap();
            cancel.put("wmsInvOutAllotmentId",param.get("originalId").toString());
            cancel.put("opUserCode", userCode);
            cancel.put("IP",IP);
            cancel.put("cancelQty",param.get("applyQty"));
            //拆包取消
            String canceldata=materialReceiptService.sortOccupyCancle(cancel);
            callInterfaceService.sendBData("sendBData", "拆包取消", canceldata, "两网传输-发送业务数据");
            log.info("拆包取消信息上传完成");
        }
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //查询拆包占用信息
        //拆包占用
        List<HashMap> allotments = packageUnpackDao.queryUnpackAllotment(map);
        for(HashMap allotment : allotments){
            String parameters = materialReceiptService.sortOccupy(userCode, IP, allotment);
            callInterfaceService.sendBData("sendBData", "两网传输-发送业务数据", parameters, "拆包占用");
            log.info("拆包占用信息上传完成");
        }

    }
    /***
     * 取消占用以及重新占用
     * */
    @Async
    public void unpackAllot2(HashMap map, String userCode, String IP) {
        //根据台账id查询占用信息
        List<HashMap> params = packageUnpackDao.queryUnpackCancelAllot(map);
        for(HashMap param : params){
            CemopCancelSupportingLedgerDTO cemopCancelSupportingLedgerDTO = new CemopCancelSupportingLedgerDTO();
            cemopCancelSupportingLedgerDTO.setMomWmsInvOutAllotmentId(param.get("originalId").toString());
            cemopCancelSupportingLedgerDTO.setUserId(userCode);
            cemopService.sendCancelAoSupportingLedger(cemopCancelSupportingLedgerDTO);
            log.info("拆包取消信息上传完成");
        }
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //查询拆包占用信息
        //拆包占用
        List<CemopSupportingLedgerDTO> allotments = packageUnpackDao.queryCemopUnpackAllotment(map);
        for(CemopSupportingLedgerDTO allotment : allotments){
            HashMap materialDTO = new HashMap();
            materialDTO.put("msTaskAoId",allotment.getAonoEcd());
            materialDTO.put("msAoMatchBillId",allotment.getAonoEcd());
            WmsInvOutBillH wmsInvOutBillH = pointPackageDao.queryWmsInvOutBillH(materialDTO);
            WmsInvOutBillL wmsInvOutBillL = pointPackageDao.queryWmsInvOutBillL(materialDTO);
            allotment.setAoVerNo(wmsInvOutBillH.getAoVersion());
            allotment.setAplyBcpltEcd(wmsInvOutBillH.getApplyShop());
            allotment.setAplyArtfcEcd(wmsInvOutBillH.getApplyUserId());
            allotment.setAplyMtlsEcd(wmsInvOutBillL.getItemCode());
            allotment.setAplyNv(String.valueOf(wmsInvOutBillL.getApplyQty()));
            allotment.setAsmblDrwnoEcd(wmsInvOutBillL.getRepartAss());
            allotment.setOpNo(wmsInvOutBillL.getOpNo());
            allotment.setProcceStusEcd(wmsInvOutBillL.getAttribute20());
            allotment.setUserId(userCode);
            cemopService.sendAoSupportingLedger(allotment);
            log.info("拆包占用信息上传完成");
        }

    }
}
